package com.example.sakurasay.passwordbook.data

import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import androidx.room.TypeConverters
import com.example.sakurasay.passwordbook.data.daos.IndependentPasswordDao
import com.example.sakurasay.passwordbook.data.daos.SharedPasswordDao
import com.example.sakurasay.passwordbook.data.entities.IndependentPasswordEntry
import com.example.sakurasay.passwordbook.data.entities.SharedAccount
import com.example.sakurasay.passwordbook.data.entities.SharedPassword

@Database(
    entities = [IndependentPasswordEntry::class, SharedPassword::class, SharedAccount::class],
    version = 2,
    exportSchema = false
)
@TypeConverters(Converters::class)
abstract class PasswordBookDatabase : RoomDatabase() {

    abstract fun independentPasswordDao(): IndependentPasswordDao
    abstract fun sharedPasswordDao(): SharedPasswordDao

    companion object {
        @Volatile
        private var INSTANCE: PasswordBookDatabase? = null

        fun getDatabase(context: Context): PasswordBookDatabase {
            return INSTANCE ?: synchronized(this) {
                val instance = Room.databaseBuilder(
                    context.applicationContext,
                    PasswordBookDatabase::class.java,
                    "password_book_database"
                ).fallbackToDestructiveMigration().build()
                INSTANCE = instance
                instance
            }
        }
    }
}